package tmproject.hlhj.fhp.cartrans.utils

import android.content.Context
import com.google.gson.Gson
import tmproject.hlhj.fhp.cartrans.javaBean.DriverInfoBean
import tmproject.hlhj.fhp.cartrans.javaBean.ExamSpBean

/**
 * Created by Never Fear   on 2018\9\1 0001.
Never More....
 */
object SpHelper {
    private val Token="token"
    private val DRIVER="driverInfo"

    private val Exam="Exam"//考试
    /**
     * 存储TOken
     */
    fun saveToken(c:Context,token:String){
        val sp = c.getSharedPreferences(Token, Context.MODE_PRIVATE)
        val edit = sp.edit()
        edit.putString("token",token)
        edit.apply()
    }

    fun getToken(c:Context): String? {
        val sp = c.getSharedPreferences(Token, Context.MODE_PRIVATE)
        return sp.getString("token",null)
    }

    /**
     *
     * 存储info
     */

    fun saveDriver(c: Context,driverInfoBean: DriverInfoBean){
        val sp = c.getSharedPreferences(DRIVER, Context.MODE_PRIVATE)
        val edit = sp.edit()
        edit.putString("driver",Gson().toJson(driverInfoBean))//存入转换后的Json
        edit.apply()
    }

    fun getDriver(c:Context): DriverInfoBean? {
        val sp = c.getSharedPreferences(DRIVER, Context.MODE_PRIVATE)
        val driVerStr = sp.getString("driver", null)
        if (null==driVerStr)return null
        val infoBean = Gson().fromJson(driVerStr, DriverInfoBean::class.java)
        return infoBean
    }
    /**
     * 存储考试信息
     */

    fun saveExam(c:Context,nowIndex:Int,rightNum:Int,examID:Int){
        val sp = c.getSharedPreferences(Exam, Context.MODE_PRIVATE)
        val edit = sp.edit()
        val examSpBean = ExamSpBean()
        examSpBean.examId=examID
        examSpBean.nowIndex=nowIndex
        examSpBean.rightNum=rightNum

        val str = Gson().toJson(examSpBean)
        edit.putString(examID.toString(),str)
        edit.apply()
    }

    fun getExam(c:Context,examID: Int): ExamSpBean? {
        val sp = c.getSharedPreferences(Exam, Context.MODE_PRIVATE)
        val str = sp.getString(examID.toString(), null)
        if (str!=null){
            return Gson().fromJson(str,ExamSpBean::class.java)
        }else{
            return null
        }
    }
}